package com.rokyinfo.pdm.controller.v1;

import com.baomidou.mybatisplus.plugins.Page;
import com.rokyinfo.pdm.controller.BasicPath;
import com.rokyinfo.pdm.entity.DDistributionRecord;
import com.rokyinfo.pdm.service.IDDistributionRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import java.net.URI;

/**
 * <p>
 * 设备生成记录表 前端控制器
 * </p>
 *
 * @author YuanZhiJian
 * @since 2017-01-22
 */
@Api(value = "/DDistributionRecords", tags = "DRecords", description = "设备生成记录")
@RestController
@RequestMapping(value = BasicPath.V1 + "/dRecords")
public class DDistributionRecordController {
    @Autowired
    private IDDistributionRecordService mIDDistributionRecordService;

    @ApiOperation(value = "获取设备生成记录列表", notes = "")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseEntity<Page<DDistributionRecord>> getDDistributionRecordList(@RequestParam(value = "offset", required = false, defaultValue = "1") Integer offset, @RequestParam(value = "limit", required = false, defaultValue = "20") Integer limit) throws Exception {

        return ResponseEntity.ok(mIDDistributionRecordService.selectDistributionRecordList(new Page<DDistributionRecord>(offset, limit)));
    }

    @ApiOperation(value = "预分配设备", notes = "预分配设备")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "", required = true, dataType = "Long", paramType = "header"),
            @ApiImplicitParam(name = "quantity", value = "", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "modelId", value = "", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "firmId", value = "", required = true, dataType = "Long", paramType = "query")
    })
    @RequestMapping(value = "/generate", method = RequestMethod.POST)
    public ResponseEntity generateDevices(@RequestHeader Long userId, Integer quantity, Long modelId, Long firmId) throws Exception {
        Long id = mIDDistributionRecordService.generateDevices( quantity,  modelId,  firmId, userId);
        URI location = ServletUriComponentsBuilder
                .fromCurrentServletMapping().path( BasicPath.V1 + "/dRecords/"+"{id}").build()
                .expand(id).toUri();
        return ResponseEntity.created(location).build();
    }

    @ApiOperation(value = "新建设备生成记录", notes = "根据DDistributionRecord对象创建设备生成记录")
    @ApiImplicitParam(name = "DDistributionRecord", value = "设备生成记录实体DDistributionRecord", required = true, dataType = "DDistributionRecord")
    @RequestMapping(value = "/", method = RequestMethod.POST)
    public ResponseEntity postDDistributionRecord(@RequestBody DDistributionRecord DDistributionRecord) {
        // 除了@ModelAttribute绑定参数之外，还可以通过@RequestParam从页面中传递参数
        if (mIDDistributionRecordService.insert(DDistributionRecord)) {
            return new ResponseEntity(HttpStatus.CREATED);
        } else {
            return ResponseEntity.unprocessableEntity().build();
        }
    }


    @ApiOperation(value = "获取设备生成记录信息", notes = "根据url的id来获取设备生成记录信息")
    @ApiImplicitParam(name = "id", value = "设备生成记录ID", required = true, dataType = "Long", paramType = "path")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ResponseEntity<DDistributionRecord> getDDistributionRecord(@PathVariable Long id) {
        DDistributionRecord mDDistributionRecord = mIDDistributionRecordService.selectById(id);
        if (mDDistributionRecord != null) {
            return ResponseEntity.ok(mDDistributionRecord);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @ApiOperation(value = "更新设备生成记录信息", notes = "根据url的id来指定更新对象，并根据传过来的DDistributionRecord信息来更新设备生成记录信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "设备生成记录ID", required = true, dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "DDistributionRecord", value = "设备生成记录实体DDistributionRecord", required = true, dataType = "DDistributionRecord")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public ResponseEntity putDDistributionRecord(@PathVariable Long id, @RequestBody DDistributionRecord DDistributionRecord) {
        DDistributionRecord.setGeneratorId(id);
        if (mIDDistributionRecordService.updateById(DDistributionRecord)) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.unprocessableEntity().build();
        }
    }


    @ApiOperation(value = "删除设备生成记录", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(name = "id", value = "设备生成记录ID", required = true, dataType = "Long", paramType = "path")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseEntity deleteDDistributionRecord(@PathVariable Long id) {
        if (mIDDistributionRecordService.deleteById(id)) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}
